package com.ruanda.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.ProdTag;
import com.ruanda.domain.ProdTagReference;
import com.ruanda.model.Result;
import com.ruanda.service.ProdTagReferenceService;
import com.ruanda.service.ProdTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品分组标签接口管理")
@RequestMapping("prod/prodTag")
@RestController
public class ProdTagController {

    @Autowired
    private ProdTagService prodTagService;
    @Autowired
    private ProdTagReferenceService prodTagReferenceService;

    /**
     * 分页查询
     * @param current  当前页
     * @param size 每页显示条数
     * @param status 状态
     * @param title 标题
     * @return
     */
    @Operation(summary = "多条件分页查询分组标签")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<Page<ProdTag>> loadProdTagPage(@RequestParam Long current,
                                                 @RequestParam Long size,
                                                 @RequestParam(required = false) Integer status,
                                                 @RequestParam(required = false) String title) {
        Page<ProdTag> page = new Page<>(current, size);
        page = prodTagService.page(page, new LambdaQueryWrapper<ProdTag>()
                .eq(ObjectUtil.isNotNull(status), ProdTag::getStatus, status)
                .like(StringUtils.hasText(title), ProdTag::getTitle, title)
                .orderByDesc(ProdTag::getSeq)
        );
        return Result.success(page);
    }

    @Operation(summary = "新增商品分组标签")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prodTag:save')")
    public Result<String> saveProdTag(@RequestBody ProdTag prodTag) {
        boolean flag = prodTagService.saveProdTag(prodTag);
        return Result.handle(flag);
    }

    @Operation(summary = "根据标识查询商品分组标签详情")
    @GetMapping("info/{tagId}")
    @PreAuthorize("hasAuthority('prod:prodTag:info')")
    public Result<ProdTag> loadProdTagInfo(@PathVariable Long tagId) {
        ProdTag prodTag = prodTagService.getById(tagId);
        return Result.success(prodTag);
    }

    @Operation(summary = "修改商品分组标签信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodTag:update')")
    public Result<String> modifyProdTag(@RequestBody ProdTag prodTag) {
        boolean flag = prodTagService.modifyProdTag(prodTag);
        return Result.handle(flag);
    }

    @Operation(summary = "删除商品分组标签")
    @DeleteMapping("{tagId}")
    @PreAuthorize("hasAuthority('prod:prodTag:delete')")
    public Result<String> removeProdTag(@PathVariable Long tagId) {
        boolean flag = prodTagService.removeById(tagId);
        return Result.handle(flag);
    }

    @Operation(summary = "查询商品分组标签集合")
    @GetMapping("listTagList")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public Result<List<ProdTag>> loadProdTagList() {
        List<ProdTag> list = prodTagService.queryProdTagList();
        return Result.success(list);
    }
    
    //////// 小程序 ////////
    @Operation(summary = "查询小程序商品分组标签")
    @GetMapping("prodTagList")
    public Result<List<ProdTag>> loadProdTagListForApp() {
        List<ProdTag> list = prodTagService.queryWxProdTagList();
        return Result.success(list);
    }

    ////////// feign ////////
    @GetMapping("getProdTagReferencePageByTagId")
    public Result<Page<ProdTagReference>> getProdTagReferencePageByTagId(@RequestParam Long current,
                                                                         @RequestParam Long size,
                                                                         @RequestParam Long tagId){
        Page<ProdTagReference> prodTagReferencePage = new Page<>(current,size);
        prodTagReferencePage = prodTagReferenceService.page(prodTagReferencePage,new LambdaQueryWrapper<ProdTagReference>()
                .eq(ProdTagReference::getTagId,tagId)
                .orderByDesc(ProdTagReference::getCreateTime)
        );
        return Result.success(prodTagReferencePage);
    }
}